Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keras Interface #54

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Keras Interface #54

wants to merge 14 commits into from

Conversation

niccololaurora
Copy link
Collaborator

This is the implementation of the Keras Interface. This PR supports only the differentiation with TensorFlow as backend.
In a future PR, I plan to enhance this Keras Interface by adding support for other types of differentiation, such as JAX.

As stated in issue #47, I had to change the Binary encoding implementation to use Symbolic Tensors.

@@ -127,6 +127,7 @@ venv/
ENV/
env.bak/
venv.bak/
qiboml/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
qiboml/

better not to ignore this

Comment on lines +1 to +21
{
// Usare IntelliSense per informazioni sui possibili attributi.
// Al passaggio del mouse vengono visualizzate le descrizioni degli attributi esistenti.
// Per altre informazioni, visitare: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debugger Python: File corrente",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"justMyCode": false,
"console": "integratedTerminal",
"env": {
"ON_HEROKU": "0",
"PYTEST_ADDOPTS": "-c pytest.ini",
"ECHO_SQL_QUERIES": "1"
},
}
]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is probably some vscode leftover, better to remove it

Comment on lines +1 to +35

import tensorflow as tf
import numpy as np
from qibo import Circuit, gates


@tf.custom_gradient
def custom_operation():
output =

def grad_fn()


return output, grad_fn



class MyLayer(tf.keras.layers.Layer):

def __init__(self):
super(MyLayer, self).__init__():
self.circuit = self.circuit()

self.weights = self.add_weights(name='w', shape=(4,), initializer="random_normal")


def circuit(self):
c = Circuit(2)
c.add(gates.X(0))
c.add(gates.RX(1, theta=0.5))

def call(self, x):
self.circuit()

def
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has to be removed as well

Comment on lines +1 to +4
[pytest]
env =
TESTING=true
ENV=local
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even this has to go I would say

@@ -44,8 +46,9 @@ def __post_init__(
self._circuit.add(gates.RY(q, theta=0.0, trainable=False))

def _set_phases(self, x: ndarray):
for gate, phase in zip(self._circuit.parametrized_gates, x.ravel()):
gate.parameters = phase
phase = tf.reshape(x, [-1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cannot have tf functions here, this has to work for pytorch as well and eventually jax when we introduce the Flax interface

Comment on lines +66 to +71
def draw(
self,
):
breakpoint()
print("ciao")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def draw(
self,
):
breakpoint()
print("ciao")

Comment on lines +1 to +29
import qibo
from qibo import gates, Circuit
import numpy as np
import tensorflow as tf


def random_subset(nqubits, k):
return np.random.choice(range(nqubits), size=(k,), replace=False).tolist()


nqubits = 3
dim = 2
backend = "tensorflow"

c = Circuit(nqubits)
c.add(gates.X(0))
c.add(gates.X(1))
c.add(gates.Z(1))
c.add(gates.CNOT(0, 1))
c.add(gates.RX(0, theta=0.4))

random_choice = random_subset(nqubits, dim)
print(f"Scelta random {random_choice}")
result = c().probabilities()
print(result)


tensor = tf.random.uniform((2, nqubits), minval=0, maxval=2, dtype=tf.int32)
print(f"Tensore: {tensor}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this test covers some case that is not tested in tests/ then better to add it there, otherwise we should remove this

Comment on lines +13 to +14
# "pytorch",
# "jax",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# "pytorch",
# "jax",
"pytorch",
"jax",

reactivate these to check that pytorch and jax are still working

]

FRONTENDS = [
"pytorch",
# "pytorch",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# "pytorch",
"pytorch",

same here

Comment on lines +41 to +58
def build_linear_layer_adding(frontend, q_model):
model = frontend.keras.Sequential()
model.add(
frontend.keras.layers.Dense(
5,
name="Dense0",
activation="relu",
input_shape=(input_size,),
)
)
model.add(q_model)
model.add(
frontend.keras.layers.Dense(
12,
name="Dense1",
activation="relu",
)
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this for? can't you use the build_sequential below?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants